package com.wedata.飞书;

import cn.hutool.json.JSONObject;
import com.wedata.HttpUtils;

import java.util.HashMap;
import java.util.Map;

public class BaseService {

    public static void main(String[] args) {
        查询记录();
    }

    public static void 查询记录(){
        //https://open.feishu.cn/open-apis/bitable/v1/apps/:app_token/tables/:table_id/records/search
        String url = "https://open.feishu.cn/open-apis/bitable/v1/apps/XxymbJ4KDaRzIms4oKAchAaandg/tables/tblCGilTKWdysXsF/records/search";
        JSONObject header = new JSONObject();
        header.put("Authorization", "Bearer " + tenantAccessToken());
        JSONObject result = HttpUtils.sendPost(url , header, new JSONObject());
        System.out.println(result);
    }
    public static void 列出数据表(){
        //https://open.feishu.cn/open-apis/bitable/v1/apps/:app_token/tables
        //https://tx3yozg1g75.feishu.cn/base/XxymbJ4KDaRzIms4oKAchAaandg?table=tblCGilTKWdysXsF&view=vew18sgXeO
        String url = "https://open.feishu.cn/open-apis/bitable/v1/apps/XxymbJ4KDaRzIms4oKAchAaandg/tables";
        Map<String, String> header = new HashMap<>();
        header.put("Authorization", "Bearer " + tenantAccessToken());
        JSONObject result = HttpUtils.doGet(url , header);
        System.out.println(result);
        //{"msg":"success","code":0,"data":{"total":1,"page_token":"tblCGilTKWdysXsF","has_more":false,"items":[{"table_id":"tblCGilTKWdysXsF","revision":52,"name":"数据表"}]}}
    }

    public static void 创建多维表格(){
        String url = "https://open.feishu.cn/open-apis/bitable/v1/apps";
        JSONObject header = new JSONObject();
        header.put("Authorization", "Bearer " + tenantAccessToken());
        JSONObject body = new JSONObject();
        body.put("app_id", "cli_a8e4b8618a3cd00b");
        body.put("app_secret", "PYcfnYKdv2OcXM9EUG3szgBFECV6Xs2k");
        JSONObject result = HttpUtils.sendPost(url, header, body);
    }
    /**
     * 应用凭证
     */
    public static String tenantAccessToken() {
        String url = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal";
        JSONObject body = new JSONObject();
        //健萌旧账号 - 健盟网络科技有限公司
        /*body.put("app_id", "cli_a8e4b8618a3cd00b");
        body.put("app_secret", "PYcfnYKdv2OcXM9EUG3szgBFECV6Xs2k");*/
        //健萌新号 - 健盟网络科技
        body.put("app_id", "cli_a8e495b61c29d00b");
        body.put("app_secret", "VfmftpnJTeJTfZt6GM3ogbSCXPGQ6MPh");
        JSONObject result = HttpUtils.sendPost(url, body);
        System.out.println(result);
        //{"msg":"ok","code":0,"tenant_access_token":"t-g10479f83MQJOPWUA43XFCNGG6KFTLTVGNJKS7QT","expire":7200}
        //{"msg":"ok","code":0,"tenant_access_token":"t-g10479f83MQJOPWUA43XFCNGG6KFTLTVGNJKS7QT","expire":7173}
        return result.getStr("tenant_access_token");
    }
}
